// JavaScript Document

var formObj;
var formInfo=[];

$(function(){
	init();
});

function init(){
	$( "#formBirthday" ).datepicker({
		changeMonth:true,
        changeYear:true,
		dateFormat:"yy-mm-dd",
		dayNamesMin:lang.week_short,
		firstDay:1,
		monthNamesShort:lang.month_short
	});
	
	formObj=$('.form').find('[id^=form]');
	
	$('#btn_clear').bind('click',function(){
		clearForm();
	});
	
	$('#btn_submit').bind('click',function(){
		save();
	});
}

function getForm(){ 
	var size=formObj.size();
	
	for(var i=0;i<size;i++){
		var obj=formObj[i];
		var id=obj.getAttribute("id");
		formInfo[id]=$.trim(formObj[i].value);
	}
}

function clearForm(){
	var size=formObj.size();
	
	for(var i=0;i<size;i++){
		formObj[i].value='';
	}	
}

function validate(){
	getForm();
	if(formInfo['formId']==""){
		alert(lang.student_id_error);
		return false;
	}else if(formInfo['formName']==""){
		alert(lang.student_name_error);	
		return false;
	}else if(formInfo['formHome']==""){
		alert(lang.student_home_error);	
		return false;
	}else if(formInfo['formPhone']==""){
		alert(lang.student_phone_error);
		return false;	
	}else if(formInfo['formBirthday']==""){
		alert(lang.student_birthday_error);	
		return false;
	}else if(formInfo['formSex']==""){
		alert(lang.student_sex_error);	
		return false;
	}else if(formInfo['formFamilyPhone']==""){
		alert(lang.student_familyphone_error);	
		return false;
	}
	
	return true;
}


function save(){
	if(!validate()) return false;
	
	ajax({
		url:'index.php',
		data:{
			m:"Manage",
			a:"studentAdd",
			id:formInfo['formId'],
			name:formInfo['formName'],
			home:formInfo['formHome'],
			phone:formInfo['formPhone'],
			birthday:formInfo['formBirthday'],
			sex:formInfo['formSex'],
			familyPhone:formInfo['formFamilyPhone'],
			nickName:formInfo['formNickname'],
			photo:formInfo['formPhoto'],
			readme:formInfo['formReadme']
		},
		callback:function(content){
			//var json=text2json(content);
			
			alert(content);
		}
	});
}